package com.github.sd4324530.fastweixin.api.extra;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * spring 支持
 */
public class SpringSupport implements InitializingBean,DisposableBean {
    private TokenCache cache;

    private ScheduledExecutorService service;

    @Override
    public void afterPropertiesSet() throws Exception {
        service = Executors.newScheduledThreadPool(5);
        service.scheduleWithFixedDelay(new TokenRefreshTask(cache), 0, TokenRefreshTask.PERIOD, TimeUnit.MILLISECONDS);
    }

    public void setCache(TokenCache cache) {
        this.cache = cache;
    }

    @Override
    public void destroy() throws Exception {
        service.shutdown();
        service = null;
        cache = null;
    }
}
